package main

import (
	"fmt"
	. "go_data_structures_and_algorithms/system_class/section11/pojo"
)

func main() {
	// 初始化一个简单的二叉树
	node1 := NewBinaryTree(0) // 根节点
	node2 := NewBinaryTree(1)
	node3 := NewBinaryTree(3)
	node4 := NewBinaryTree(4)
	node5 := NewBinaryTree(5)
	node1.Left = node2
	node1.Right = node3
	node2.Left = node4
	node2.Right = node5

	// 前序遍历这个二叉树
	fmt.Print("前序遍历: ")
	ergodic := FrontErgodic(node1)
	fmt.Println(ergodic)

	fmt.Print("中序遍历: ")
	ergodic1 := MidErgodic(node1)
	fmt.Println(ergodic1)

	fmt.Print("后序遍历: ")
	ergodic2 := BehindErgodic(node1)
	fmt.Println(ergodic2)
}

func FrontErgodic(tree *BinaryTree) []int {
	arr := []int{}
	PreDpf(tree, &arr)
	return arr
}

func PreDpf(Node *BinaryTree, arr *[]int) {

	if Node == nil {
		return
	}
	*arr = append(*arr, Node.Value)
	PreDpf(Node.Left, arr)
	PreDpf(Node.Right, arr)
}

func MidErgodic(tree *BinaryTree) []int {
	arr := []int{}
	MidDpf(tree, &arr)
	return arr
}

func MidDpf(Node *BinaryTree, arr *[]int) {

	if Node == nil {
		return
	}

	MidDpf(Node.Left, arr)
	*arr = append(*arr, Node.Value)
	MidDpf(Node.Right, arr)
}

func BehindErgodic(tree *BinaryTree) []int {
	arr := []int{}
	BehindDpf(tree, &arr)
	return arr
}

func BehindDpf(Node *BinaryTree, arr *[]int) {

	if Node == nil {
		return
	}

	BehindDpf(Node.Left, arr)

	BehindDpf(Node.Right, arr)

	*arr = append(*arr, Node.Value)
}
